<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\file\FileInput;
use \kucha\ueditor\UEditor;
use common\models\articles\Articles;
use common\models\category\Category;
use yii\helpers\ArrayHelper;
use common\models\modelled\Modelled;
/* @var $this yii\web\View */
/* @var $model common\models\articles\Articles */
/* @var $form yii\widgets\ActiveForm */

$modelled = Modelled::find()->andWhere(['category'=>'文章管理'])->asArray()->all();
?>
<style>
.kv-file-content img{ width: 100%!important; }
</style>
<script type="text/javascript" src="<?= yii::getAlias('@web');?>/js/jquery.js"></script>
<div class="articles-form">

    <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data'],'id'=>'subform']); ?>
<!--去掉了where（['status']=>1）-->
<?= $form->field($model, 'category_id')->label('分类')->dropDownList(ArrayHelper::map(Category::get(0, Category::find()->asArray()->all()), 'id', 'str_label')) ?>
 <div class="row">
<div class="col-md-4">
    <?= $form->field($model, 'title')->textInput(['maxlength' => 1000, 'oninput' => "myFunction()"]) ?>
</div>
<div class="col-md-4">
    <label>标题还可输入字符数</label>
    <input id="titlenum" type="text" class="form-control" value="1000" />
    <input id="cont" name="Articles[cont]" type="hidden" class="form-control" value="" />
<script>
function myFunction() {
    var p = $("#articles-title").val().trim();
    var len = 1000 - p.length;
    $("#titlenum").val(len);
}
</script>
</div>

</div>
    <?= $form->field($model, 'subhead')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'intro')->textarea(['rows' => 6]) ?>

        <?php
            if(isset(json_decode($model->img)->newname)){
                $img = json_decode($model->img)->newname;
            }else{

                $img = $model->img;
            }

            $siteRoot = Yii::$app->params['qtimg'];
            echo '<label class="control-label">图片</label>';
            echo FileInput::widget([
                'model' => $model,
                'attribute' => 'img',
                'options' => ['accept' => 'image/*'],
                'pluginOptions' => [
                    'uploadExtraData' => [
                        'album_id' => 20,
                        'cat_id' => 'Nature'
                    ],
                    'initialPreview'=>[
                        !empty($img)?Html::img(Yii::$app->params['qtimg'].$img):null,
                    ],
                    'initialCaption'=> $img,
                    'maxFileCount' => 10,
                    "showUpload"=> false,
                ]
            ]);
        ?>

        <?php
            if(isset(json_decode($model->file)->newname)){
                $file = json_decode($model->file)->newname;
            }else{

                $file = $model->file;
            }

            $siteRoot = Yii::$app->params['articlefile'];
            echo '<label class="control-label">文件上传</label>';
            echo FileInput::widget([
                'model' => $model,
                'attribute' => 'file',
                'options' => ['accept' => '*'],
                'pluginOptions' => [
                    'uploadExtraData' => [
                        'album_id' => 20,
                        'cat_id' => 'Nature'
                    ],
                    'initialPreview'=>[
                        !empty($file)?Html::file(Yii::$app->params['articlefile'].$file):null,
                    ],
                    'initialCaption'=> $file,
                    'maxFileCount' => 10,
                    "showUpload"=> false,
                ]
            ]);
        ?>
    <?= $form->field($model, 'author')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'comefrom')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'link')->textInput(['maxlength' => true]) ?>
    <?php
    if(isset($modelled)){
        foreach($modelled as $key=>$val){
            $valname = '';
            $decode = json_decode($model->optiontag,true);
            if($decode){
                $keyiset = array_key_exists($val['name'], $decode);

                if($keyiset){
                    $valname = $decode[$val['name']];
                }
            }

            echo  Modelled::createform($val['option'],$val['name'],'tag['.$val['name'].']',  $valname);
        }
    }

    ?>
<?=
 $form->field($model,'content')->widget('kucha\ueditor\UEditor',[]);
?>

<div class="row">

    <div class="col-md-4">
        <input id="summarycheck" type="checkbox" class="dese" >
        <label>是否截取内容</label>
        <input class="fontsize" type="text" value="200" style="width: 40px;" />
        <label>字符至内容摘要</label>
    </div>
        <script>
            $(document).ready(function(){
              $(".dese").click(function(){

                    if($('.dese').is(':checked')) {
                        var txt = $('textarea[name="Articles[content]"]').val();
                        var start_ptn = /<\/?[^>]*>/g;
                        var end_ptn = /[ | ]*\n/g;
                        var space_ptn = /&nbsp;/ig;
                        var newtxt = txt.replace(start_ptn,"").replace(end_ptn).replace(space_ptn,"");
                        var SIZE = $(".fontsize").val();
                        $("#articles-intro").val(newtxt.substr(0,SIZE));
                };

              })
            }) ;
        </script>
    <div class="col-md-4">
        <input id="cover" name="Articles[cover]" type="hidden" value="" />
        <input id="covercheck" type="checkbox" class="coverdese" >
        <label>是否截取内容第</label>
        <input class="coverfontsize" type="text" value="1" style="width: 20px;" />
        <label>张图片作为标题图片</label>
    </div>
        <script>
            $(document).ready(function(){
              $(".coverdese").click(function(){
                    if($('.coverdese').is(':checked')) {
                        var index = $(".coverfontsize").val();
                        var txt = $('textarea[name="Articles[content]"]').val();
                        var reg = /src=\"[^>]*\"\/>/gi;
                        var res = txt.match(reg);

                        if(res != null){
                            var leng = txt.match(reg)['length'];
                            
                            if((index-1) < leng){
                                var b = res[(index-1)];
                                console.log(b);
                                $("#cover").val(b);

                            } else {
                            alert("没有第 "+index+" 张图片");
                            $("#covercheck").attr("checked", false);
                            }
                        } else {
                            alert("没有第 "+index+" 张图片");
                            $("#covercheck").attr("checked", false);
                        }
                    };
              })
            }) ;
        </script>
</div>

    <?= $form->field($model, 'seokeyword')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'seocontent')->textarea(['rows' => 6]) ?>

    <?= $form->field($model, 'created_at')->textInput(['value' => date('Y-m-d H:i:s', time())]) ?>

    <?= $form->field($model, 'front_show')->dropDownList(['1' => '是', '0' => '否']) ?>

    <?= $form->field($model, 'isrecom')->dropDownList(['是' => '是', '否' => '否']) ?>	
	<?= $form->field($model, 'push')->dropDownList(['1' => '是', '0' => '否']) ?>
	
    <?= $form->field($model, 'status')->dropDownList(Articles::getArrayStatus()) ?>
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', '保存') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
        <?php if($model->isNewRecord) { ?>
        <?= Html::Button(Yii::t('app', '保存后继续添加'), ['class' => 'btn btn-success', 'id' => 'continue']) ?>
        <?php } ?>
    </div>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#continue").click(function(){
                $("#cont").val(1);
                var ttt = $("#cont").val();
                $("#subform").submit();
            })

        });
    </script>

    <?php ActiveForm::end(); ?>

</div>
